package com.formula.datastructure.exam.tree;

public class BinLinkNode {
    public Integer data;
    public int idx;
    public BinLinkNode left;
    public BinLinkNode right;

    public void inOrder() {
        if (left != null) {
            left.visit();
        }
        visit();
        if (right != null) {
            right.visit();
        }
    }

    public int depth() {
        int a = 0;
        int b = 0;
        if (left != null) {
            a = left.depth();
        }
        if (right != null) {
            b = right.depth();
        }

        return 1 + (a > b ? a : b);
    }

    public int singleChild() {
        int a = 0;
        int b = 0;
        if (left != null && right != null) {
            a = left.singleChild();
            b = right.singleChild();
            return a + b;
        } else if (left != null) {
            a = left.singleChild();
            return 1 + a;
        } else if (right != null) {
            b = right.singleChild();
            return 1 + b;
        }
        return 0;
    }

    public int doubleKids() {
        if (left != null && right != null) {
            int a = left.doubleKids();
            int b = right.doubleKids();
            return 1 + a + b;
        } else if (left == null && right == null) {
            return 0;
        } else if (left != null) {
            return left.doubleKids();
        } else {
            return right.doubleKids();
        }
    }

    public int leafNum() {
        if (left == null && right == null) {
            return 1;
        }
        int a = 0;
        int b = 0;
        if (left != null) {
            a = left.leafNum();
        }
        if (right != null) {
            b = right.leafNum();
        }
        return a + b;
    }


    private void visit() {
        System.out.println(data);
    }
}
